package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    //注入实现spring提供的repository对象
    private TravelCommentMongoRepository repository;

    @Override
    public TravelComment save(TravelComment comment) {
        //判断是评论还是回复
        if (TravelComment.TRAVEL_COMMENT_TYPE == comment.getType()){
            //回复
            //获取被回复的评论
            Optional<TravelComment> op = repository.findById(comment.getRefComment().getId());
            //非空判断
            if (op.isPresent()) {
                TravelComment travelComment = op.get();
                //将评论信息设置进回复中
                comment.setRefComment(travelComment);
            }
        }
        //设置时间
        comment.setCreateTime(new Date());

        //保存操作
        repository.save(comment);
        return comment;
    }

    @Override
    public List<TravelComment> findByTravelId(Long travelId) {
        return repository.findByTravelId(travelId);
    }

}
